package com.quarters.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.quarters.bean.ArticleOrder;
import com.quarters.bean.NoticeOrder;
import com.quarters.bean.User;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.bean.request.SendArticles;
import com.quarters.bean.request.SendNotice;
import com.quarters.mapper.NoticeOrderMapper;
import com.quarters.mapper.UserMapper;
import com.quarters.service.NoticeOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quarters.service.XbxWxService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.quarters.serviceImpl.InquiryServiceImpl.stampToDate;

/**
 * <p>
 * 通知订单表 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-11
 */
@Service
public class NoticeOrderServiceImpl extends ServiceImpl<NoticeOrderMapper, NoticeOrder> implements NoticeOrderService {

    @Autowired
    XbxWxService xbxWxService;
    @Autowired
    NoticeOrderMapper noticeOrderMapper;
    @Autowired
    UserMapper userMapper;

    /**
     * @param voPage
     * @return 后台查询
     */
    @Override
    public ResultPage<NoticeOrder> selectMyPage(VoPage voPage, Long userId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("userId", userId);
        List<NoticeOrder> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<NoticeOrder> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
    @Override
    public ResultPage<NoticeOrder> selectMyPageAll(VoPage voPage) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<NoticeOrder> list = baseMapper.selectMyPageAll(paramMap);
        Integer count = baseMapper.countMyPageAll(paramMap);
        ResultPage<NoticeOrder> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * @param userId
     * @param time
     * @return 根据当前用户今天发送多少次群发
     */
    @Override
    public List<NoticeOrder> selectByIdTime(Long userId, String time) {
        return baseMapper.selectByIdTime(userId, time);
    }
    /**
     * @param noticeOrder
     * @return 支付
     * @throws WxErrorException
     * @throws WxPayException
     */
    @Override
    public WxPayMpOrderResult successPay(NoticeOrder noticeOrder) throws WxErrorException, WxPayException {
        BigDecimal bigDecimal2 = new BigDecimal(noticeOrder.getOrderMoeny());
        //生成订单数据
        System.out.println("钱:"+bigDecimal2.intValue());
        System.out.println("订单号:"+noticeOrder.getOrderNumber());
        System.out.println("付款openid:"+noticeOrder.getOpenid());
        WxPayMpOrderResult payData = xbxWxService.mpPayOrder(bigDecimal2.intValue(),noticeOrder.getOrderNumber(),noticeOrder.getOpenid(),2);
        System.out.println("订单数据:"+payData);
        return payData;
    }


    /**
     * @param myOrderId
     * @param wxOrderId
     * @param totalFee
     *  订单成功给区域管理员发送消息
     */
    @Override
    public void paySuccess(String myOrderId, String wxOrderId, String totalFee) {
        //根据商户订单号查询出订单记录
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_number", myOrderId);
        NoticeOrder order = this.getOne(queryWrapper);
        order.setMoeny(totalFee);
        order.setFlage(1);
        this.updateById(order);
        //给用户发送模板消息
        NoticeOrder noticeOrder =  noticeOrderMapper.selectByOrderNumber(myOrderId);
        String userIds[] = noticeOrder.getUserList().split(",");
        for (int i = 0; i < userIds.length; i++) {

            User user = userMapper.selectByIdUser(Long.parseLong(userIds[i]));
            //给用户发送通知
            SendNotice sendArticles = new SendNotice();
            //推送标题
            sendArticles.setContent(noticeOrder.getContent());
            //创建时间
            sendArticles.setCreateTime(stampToDate(noticeOrder.getCreateTime() + ""));
            //推送人
            sendArticles.setToUser(user.getOpenid());
            sendArticles.setNoticeId(noticeOrder.getNoticeId());

            try {
                xbxWxService.sendNotice(sendArticles);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }
    }




    //订单查询
    @Override
    public ResultPage<NoticeOrder> selectOrderAll(VoPage voPage,String time) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("time", time);
        List<NoticeOrder> list = baseMapper.selectOrderAll(paramMap);
        Integer count = baseMapper.countOrderAll(paramMap);
        ResultPage<NoticeOrder> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    @Override
    public ResultPage<NoticeOrder> selectOrder(VoPage voPage,Long userId,String time) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("userId", userId);
        paramMap.put("time", time);
        List<NoticeOrder> list = baseMapper.selectOrder(paramMap);
        Integer count = baseMapper.countOrder(paramMap);
        ResultPage<NoticeOrder> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * @param voPage
     * @param userId
     * @return 标签管理员查询订单
     */
    @Override
    public ResultPage<NoticeOrder> selectLableOrder(VoPage voPage,Long userId,String time ) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("userId", userId);
        paramMap.put("time", time);
        List<NoticeOrder> list = baseMapper.selectLableOrder(paramMap);
        Integer count = baseMapper.countLableOrder(paramMap);
        ResultPage<NoticeOrder> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

}
